/*LICENSE_BEGIN
 * ******************************************************************
 * All original parts of SoccerScope2003 have copyright (2003) by
 * Team YowAI, represented by Mr. Shuhei Shiota
 * The Department of Creative Informatics
 * The Graduate School of Information Science and Technology
 * The University of Tokyo
 * 
 * Additional classes (except libraries in /lib) were developed by 
 * the Dainamite Robocup-Team of the DAI-Labor, TU-Berlin 
 * (www.dai-labor.de)
 * 
 * Extended/new classes/packages are:
 *   soccerscope.analyzer.LogfileAnalyzer
 *   soccerscope.daistatistic.*
 *   soccerscope.daistatistic.charts.*
 *   soccerscope.daistatistic.tools.*
 *   soccerscope.db.*
 *   soccerscope.dialog.*
 *   soccerscope.model.PlayerWorldModel
 *   soccerscope.model.SimplePlayer
 *   soccerscope.net.AgentConnector
 *   soccerscope.training.*
 *   soccerscope.training.conditions.*
 * ******************************************************************
LICENSE_END*/
package soccerscope.training.conditions;

import robocup.component.geometry.Vektor;
import soccerscope.model.ClientScene;

public class BallPossessionCondition extends GameCondition {

	boolean leftTeam = false;

	public BallPossessionCondition() {
		super();
	}

	public void setLeftTeam(boolean leftTeam) {
		this.leftTeam = leftTeam;
	}

	public boolean isLeftTeam() {
		return this.leftTeam;
	}

	/**
	 * This method checks wheather the specified Team is in Ball-Possession. For
	 * performance and simplicity reasons, an estimation is used here. If the
	 * Team is in direct ball-possession, i.e., a player can kick the ball
	 * without the ability of another teams player can do so, this condition is
	 * true.
	 */
	@Override
	public boolean isTrue(ClientScene scene) {
		Vektor ball = new Vektor(Vektor.XY, scene.ball.pos.y, scene.ball.pos.x);

		double dist = 1;
		int pNum = -1;

		for (int i = 0; i < scene.player.length; i++) {
			Vektor player = new Vektor(Vektor.XY, scene.player[i].pos.y,
					scene.player[i].pos.x);
			double pDist = player.getDistance(ball);
			if (pDist < dist) {
				pNum = i;
				if (this.leftTeam && pNum >= 11)
					return false;
				if (!this.leftTeam && pNum < 11)
					return false;
			}
		}

		if (pNum == -1)
			return false;
		// System.out.println("" + WorldModel.getInstance().
		// getSceneSet().lastScene().time +
		// " Ball-Possession-Condition");
		return true;

	}

	/**
	 * Nothing to reset in the moment
	 */
	@Override
	public void resetCondition() {
		return;
	}

	public int getStatIndex() {
		return 2;
	}

}
